//
//  BRRentPreViewCard.m
//  OceanCharge
//
//  Created by rjb-iOS on 2024/12/11.
//

#import "BRRentPreViewCard.h"

@implementation BRRentPreViewCard

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)toPrePay:(QMUIButton *)btn {
    
    [self hiddenSelf];
    
    NSInteger index = btn == self.appleBtn ? 0 : 1;
    
    if (self.prePayClickBlock) self.prePayClickBlock(index);
    
    
    [self hiddenSelf];
        

    
    if (self.cardPayClickBlock) {
        self.cardPayClickBlock();
    }
    
}

- (void)hiddenSelf {
    [UIView animateWithDuration:0.25 animations:^{
        self.contView.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)showSelf {
    
    self.hidden = NO;
    
    CGFloat h = self.contView.mj_h - 33;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.contView.transform = CGAffineTransformMakeTranslation(0, -h);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)toAddDeposit:(QMUIButton *)btn {
    if (self.toDepositBlock) {
        self.toDepositBlock();
    }
}


- (void)setDepositAmount:(NSDecimalNumber *)depositAmount {
    _depositAmount = depositAmount;
    
    NSString *preTitle = [NSString stringWithFormat:Localized(@"rent_continueDeposit"),[BRPayManager br_amount:depositAmount]];
    [self.depositBtn setTitle:preTitle forState:UIControlStateNormal];
}


-(NSString*)formatterBankCardNum:(NSString *)text{
    
    NSString*lastStr;
    
    // 确保输入的文本是数字
        NSCharacterSet *doNotWant = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
        NSString *cleanText = [[text componentsSeparatedByCharactersInSet:doNotWant] componentsJoinedByString:@""];
        
        // 如果文本长度大于12，则显示前12位为星号，后四位数字一组
        if (cleanText.length > 12) {
            NSString *stars = [NSString stringWithFormat:@"%@**********", [cleanText substringToIndex:12]];
            NSMutableString *formattedString = [NSMutableString stringWithString:stars];
            [formattedString appendString:[cleanText substringFromIndex:cleanText.length - 4]];
            lastStr = formattedString;
            
        } else {
            lastStr = text;
        }
    return lastStr;
}

#pragma mark - UI --
- (void)setUI {
    self.hidden = YES;
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.shadowView];
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [self addSubview:self.contView];
    [self.contView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(self.mas_bottom).offset(0);
    }];
    
    QMUILabel *titleL = [BRToolsView br_label:Localized(@"rent_chooseAuthMethod") textColor:kColorTitle font:[UIFont br_fontM:14] textAlignment:NSTextAlignmentCenter];
    titleL.numberOfLines = 0;
    
    
    
    
    [self.contView addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16);
        make.left.offset(28);
        make.right.offset(-28);
        
    }];
    
    QMUIButton *close = [[QMUIButton alloc] init];
    [close setImage:UIImageMake(@"arrow_close_gray44") forState:UIControlStateNormal];
    [self.contView addSubview:close];
    [close mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.width.height.mas_equalTo(52);
        make.left.offset(6);
    }];
    [close addTarget:self action:@selector(hiddenSelf) forControlEvents:UIControlEventTouchUpInside];
    
    UIStackView *stack = [[UIStackView alloc] init];
    stack.axis = UILayoutConstraintAxisVertical;
    stack.spacing = 15;
    [self.contView addSubview:stack];
    [stack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleL.mas_bottom).offset(34);
        make.left.offset(28);
        make.right.offset(-28);
        make.bottom.offset(-(33 + kSafeAreaBottomHeight + 16));
    }];
    
    
    self.cardBtn = [[QMUIButton alloc] init];

    [self.cardBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.cardBtn.titleLabel.font = [UIFont br_fontB:17];
    [self.cardBtn setBackgroundColor:UIColorMakeWithHex(@"#F5F5F7")];
    self.cardBtn.layer.cornerRadius = 26;
    self.cardBtn.layer.masksToBounds = YES;
    
    [stack addArrangedSubview:self.cardBtn];

    [self.cardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
    }];
    
    UIImageView* carImg = [[UIImageView alloc] init];
    carImg.image = [UIImage imageNamed:@"ic_bankcard"];
    [self.cardBtn addSubview:carImg];
    
    [carImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(44);
        make.size.mas_equalTo( CGSizeMake(44,44));
        make.centerY.equalTo(self.cardBtn);
    }];
    
    self.cardLab = [[UILabel alloc] init];
//    self.cardLab.text =@"**** **** **** 0023";
    self.cardLab.font = [UIFont fontS:14];
    [self.cardBtn addSubview:self.cardLab];
    
    [self.cardLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(carImg.mas_right).offset(14);
        make.centerY.equalTo(self.cardBtn);
    }];
    
    
    NSString *terms = Localized(@"rent_showAll");
    NSMutableAttributedString *agreeAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",terms]];
    
    [agreeAttStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, terms.length) ];
    
    QMUIButton *allBtn = [[QMUIButton alloc] init];
    [allBtn setTitle:terms forState:UIControlStateNormal];
    [allBtn setTitleColor:kColorTitle forState:UIControlStateNormal];
    allBtn.titleLabel.font = [UIFont br_fontM:14];
    [allBtn setAttributedTitle:agreeAttStr forState:UIControlStateNormal];
    [stack addArrangedSubview:allBtn];
    [allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(100);
    }];
    
    [allBtn addTarget:self action:@selector(labelTapped) forControlEvents:UIControlEventTouchUpInside];
    
    [stack addSubview:self.recordView];
    
    [self.recordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(stack.mas_right).offset(-24);
        make.top.offset(-10);
    }];
    
    
    [self.appleBtn addTarget:self action:@selector(toPrePay:) forControlEvents:UIControlEventTouchUpInside];
    [self.cardBtn addTarget:self action:@selector(toPrePay:) forControlEvents:UIControlEventTouchUpInside];
    [self.depositBtn addTarget:self action:@selector(toAddDeposit:) forControlEvents:UIControlEventTouchUpInside];
    
//    WS(weakSelf);
    [self.shadowView jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
//        [weakSelf hiddenSelf];
    }];
    
    [self.contView layoutIfNeeded];
    
    
}

- (void)toPreVsCardPay:(QMUIButton *)btn {
    
    [self hiddenSelf];
        

    
    if (self.cardPayClickBlock) {
        self.cardPayClickBlock();
    }
}


- (void)labelTapped {

    // 在这里处理点击事件
    if (self.agreeMsgBlock) {
        self.agreeMsgBlock();
    }
}


-(void)setCardModel:(BRCardModel *)cardModel{
    
    if (cardModel) {
        
        self.cardLab.text =[NSString stringWithFormat:@"**** **** **** %@",cardModel.bankNo];
    }
}


- (UIView *)shadowView {
    
    if (_shadowView == nil) {
        _shadowView = [[UIView alloc] init];
        _shadowView.backgroundColor = kColorShadow(0.75);
    }
    return _shadowView;
}

- (UIView *)contView {
    
    if (_contView == nil) {
        _contView = [[UIView alloc] init];
        _contView.backgroundColor = UIColor.whiteColor;
        _contView.layer.cornerRadius = 33;
        _contView.layer.masksToBounds = YES;
    }
    return _contView;
}


- (BRLoginRecordView *)recordView {
    
    if (_recordView == nil) {
        _recordView = [[BRLoginRecordView alloc] init];
        _recordView.contV.backgroundColor = kUIColorMainTint;
        _recordView.titleL.textColor = kColorMain;
        _recordView.bImage.hidden = YES;
    
    }
    return _recordView;
}
@end
